package com.dangdi.show.test;

import java.awt.*;
import java.awt.print.*;
import java.util.Map;

public class testPrinter implements Printable {
    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if (pageIndex > 0) {
            return NO_SUCH_PAGE;
        }
        Graphics2D g2d = (Graphics2D) graphics;
        g2d.setFont(new Font("Default", Font.PLAIN, 14));
        g2d.drawString("----------------小票打印-------------", 7, 20);
        g2d.drawString("小田田", 60, 40);
        g2d.drawString("-------------------------------------", 7, 60);
        g2d.drawString("手机号码：" + "17602258294", 7, 80);
        g2d.drawString("领号日期：" + "11111", 7, 100);
        g2d.drawString("-------------------------------------", 7, 120);
        return PAGE_EXISTS;
    }

    public void printTicket(Map<String, Object> map) {

        int height = 175 + 3 * 15 + 20;
        Book book = new Book();
        PageFormat pf = new PageFormat();
        pf.setOrientation(PageFormat.PORTRAIT);
        Paper p = new Paper();
        p.setSize(230, height);
        p.setImageableArea(5, -20, 230, height + 20);
        pf.setPaper(p);
        book.append(new testPrinter(), pf);
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPageable(book);

        try {
            job.print();
        } catch (PrinterException e) {
            System.out.println("打印失败");
            e.printStackTrace();
        }
    }
}
